package com.molichuxing.gateway.api.services.impl;

import com.molichuxing.framework.exception.BizException;
import com.molichuxing.framework.result.ResponseCodeEnum;
import com.molichuxing.framework.result.ResponseResult;
import com.molichuxing.gateway.api.services.OrderApiService;
import com.molichuxing.gateway.bff.financial.entity.response.ContractRenegeParamsVo;
import com.molichuxing.gateway.bff.financial.services.ContractBffService;
import com.molichuxing.gateway.bff.orders.entity.request.create.*;
import com.molichuxing.gateway.bff.orders.entity.request.modify.*;
import com.molichuxing.gateway.bff.orders.entity.response.*;
import com.molichuxing.gateway.bff.orders.services.*;
import com.molichuxing.gateway.bff.risk.services.RiskBffService;
import com.molichuxing.gateway.bff.user.entity.response.AccountVo;
import com.molichuxing.gateway.bff.user.entity.response.UserVo;
import com.molichuxing.gateway.bff.user.services.UserBffService;
import com.molichuxing.gateway.exception.OrderBffExceptionCodeEnum;
import com.molichuxing.gateway.property.OrderCreateTypeEnum;
import com.molichuxing.gateway.utils.redis.TokenUtil;
import com.molichuxing.services.property.OrderSourceEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@Api(tags = "订单相关接口")
@RestController
@RequestMapping(value = "/order")
public class OrderApiServiceImpl extends RootApiService implements OrderApiService {
	private static final Logger logger = LoggerFactory.getLogger(OrderApiServiceImpl.class);

	@Resource
	private TokenUtil tokenUtil;

	@Resource
	private OrderBffService orderBffService;

	@Resource
	private OrderScheduleBffService orderScheduleBffService;

	@Resource
	private OrderTradeBffService orderTradeBffService;

	@Resource
	private OrderReturnBffService orderReturnBffService;

	@Resource
	private OrderRenegeBffService orderRenegeBffService;

	@Resource
	private ContractBffService contractBffService;

	@Resource
	private RiskBffService riskBffService;

	@Resource
	private UserBffService userBffService;

	@Override
	@ApiOperation(value = "订单列表-移动端")
	@GetMapping(value = "/list")
	public ResponseResult<List<OrderMobileListVo>> orderList(
			@RequestHeader(value = "token", required = false) String token,
			@RequestHeader(value = "appVersion", required = false) String appVersion) {
		ResponseResult<List<OrderMobileListVo>> result = new ResponseResult<>();
		try {
			// 根据token获取用户信息
			UserVo user = tokenUtil.getUser(token);

			// 根据用户id获取订单列表信息
			result.success(orderBffService.getMobileList(user.getUserId(), appVersion));

		} catch (BizException e) {
			logger.error("[orderList]查询订阅里程订单列表信息异常:", e);
			result.error(e.getExceptionCode() == null ? ResponseCodeEnum.UNKNOWN_ERROR.getCode() : e.getExceptionCode(),
					e.getMessage());
		} catch (Exception e) {
			logger.error("[orderList]查询订单列表信息异常:", e);
			result.error("查询订单列表信息异常");
		}

		return result;
	}

	@Override
	@ApiOperation(value = "订单详情-专属里程")
	@ApiImplicitParams({ @ApiImplicitParam(name = "orderCode", value = "订单号", required = true, dataType = "Long") })
	@GetMapping(value = "/exclusivel/details")
	public ResponseResult<OrderExclusivelDetailsVo> orderExclusivelDetails(
			@RequestHeader(value = "token", required = false) String token,
			@RequestHeader(value = "appVersion", required = false) String appVersion, Long orderCode) {
		ResponseResult<OrderExclusivelDetailsVo> result = new ResponseResult<>();
		try {
			// 根据token获取用户信息
			UserVo user = tokenUtil.getUser(token);

			// 获取专属里程订单详情信息
			result.success(orderBffService.getExclusivelDetails(user.getUserId(), orderCode, appVersion));

		} catch (BizException e) {
			logger.error("[orderExclusivelDetails]查询专属里程订单列表信息异常:", e);
			result.error(e.getExceptionCode() == null ? ResponseCodeEnum.UNKNOWN_ERROR.getCode() : e.getExceptionCode(),
					e.getMessage());
		} catch (Exception e) {
			logger.error("[orderExclusivelDetails]查询专属里程订单详情信息异常:", e);
			result.error("查询订单详情信息异常");
		}

		return result;
	}

	@Override
	@ApiOperation(value = "订单详情-订阅里程")
	@ApiImplicitParams({ @ApiImplicitParam(name = "orderCode", value = "订单号", required = true, dataType = "Long") })
	@GetMapping(value = "/subscribe/details")
	public ResponseResult<OrderSubscribeDetailsVo> orderSubscribeDetails(
			@RequestHeader(value = "token", required = false) String token,
			@RequestHeader(value = "appVersion", required = false) String appVersion, Long orderCode) {
		ResponseResult<OrderSubscribeDetailsVo> result = new ResponseResult<>();
		try {
			// 根据token获取用户信息
			UserVo user = tokenUtil.getUser(token);

			// 获取专属里程订单详情信息
			result.success(orderBffService.getSubscribeDetails(user.getUserId(), orderCode, appVersion));

		} catch (BizException e) {
			logger.error("[orderSubscribeDetails]查询订阅里程订单列表信息异常:", e);
			result.error(e.getExceptionCode() == null ? ResponseCodeEnum.UNKNOWN_ERROR.getCode() : e.getExceptionCode(),
					e.getMessage());
		} catch (Exception e) {
			logger.error("[orderSubscribeDetails]查询订阅里程订单详情信息异常:", e);
			result.error("查询订单详情信息异常");
		}

		return result;
	}


	@Override
	@ApiOperation(value = "订单详情-专属里程-还款明细")
	@ApiImplicitParams({ @ApiImplicitParam(name = "orderCode", value = "订单号", required = true, dataType = "Long") })
	@GetMapping(value = "/exclusivel/repayment/list")
	public ResponseResult<RepaymentVo> exclusivelRepaymentList(
			@RequestHeader(value = "token", required = false) String token, Long orderCode) {
		ResponseResult<RepaymentVo> result = new ResponseResult<>();
		try {
			// 根据token获取用户信息
			UserVo user = tokenUtil.getUser(token);

			// 获取专属里程订单详情信息
			result.success(orderScheduleBffService.getExclusivelRepayment(user.getUserId(), orderCode));

		} catch (BizException e) {
			logger.error("[exclusivelRepaymentList]查询专属里程还款明细列表信息异常:", e);
			result.error(e.getExceptionCode() == null ? ResponseCodeEnum.UNKNOWN_ERROR.getCode() : e.getExceptionCode(),
					e.getMessage());
		} catch (Exception e) {
			logger.error("[exclusivelRepaymentList]查询专属里程还款明细信息异常:", e);
			result.error("查询还款明细信息异常");
		}

		return result;
	}

	@Override
	@ApiOperation(value = "生成专属里程订单")
	@PostMapping(value = "/exclusivel/create")
	public ResponseResult<OrderVo> createOrderExclusivel(@RequestHeader(value = "token", required = true) String token,
			@RequestHeader(value = "channel", required = true) String source,
			@RequestHeader(value = "deviceNum", required = true) String deviceNum,
			@RequestBody @Valid OrderExclusivelCreateVo createVo, BindingResult bindingResult) {
		ResponseResult<OrderVo> result = new ResponseResult<>();
		try {
			// 生成类型
			createVo.setCreateType(OrderCreateTypeEnum.EXCLUSIVEL);
			// 新增
			result.success(this.createOrder(token, source.toUpperCase(), deviceNum, createVo));

		} catch (BizException e) {
			logger.error("[createOrderExclusivel]新增专属里程订单异常:", e);
			result.error(e.getExceptionCode() == null ? ResponseCodeEnum.UNKNOWN_ERROR.getCode() : e.getExceptionCode(),
					e.getMessage());
		} catch (Exception e) {
			logger.error("[createOrderExclusivel]新增专属里程订单异常:", e);
			result.error("新增订单异常");
		}

		return result;
	}

	@Override
	@ApiOperation(value = "生成订阅里程订单")
	@PostMapping(value = "/subscribe/create")
	public ResponseResult<OrderVo> createOrderSubscribe(@RequestHeader(value = "token", required = true) String token,
			@RequestHeader(value = "channel", required = true) String source,
			@RequestHeader(value = "deviceNum", required = true) String deviceNum,
			@RequestBody @Valid OrderSubscribeCreateVo createVo, BindingResult bindingResult) {
		ResponseResult<OrderVo> result = new ResponseResult<>();
		try {
			// 生成类型
			createVo.setCreateType(OrderCreateTypeEnum.SUBSCRIBE);
			// 新增
			result.success(this.createOrder(token, source.toUpperCase(), deviceNum, createVo));

		} catch (BizException e) {
			logger.error("[createOrderSubscribe]新增订阅里程订单异常:", e);
			result.error(e.getExceptionCode() == null ? ResponseCodeEnum.UNKNOWN_ERROR.getCode() : e.getExceptionCode(),
					e.getMessage());
		} catch (Exception e) {
			logger.error("[createOrderSubscribe]新增订阅里程订单异常:", e);
			result.error("新增订单异常");
		}

		return result;
	}

	@Override
	@ApiOperation(value = "生成加购订单")
	@PostMapping(value = "/purchase/create")
	public ResponseResult<OrderVo> createOrderPurchase(@RequestHeader(value = "token", required = true) String token,
			@RequestHeader(value = "channel", required = true) String source,
			@RequestHeader(value = "deviceNum", required = true) String deviceNum,
			@RequestBody @Valid OrderPurchasedCreateVo createVo, BindingResult bindingResult) {
		ResponseResult<OrderVo> result = new ResponseResult<>();
		try {
			// 验证驾驶证
			ResponseResult<String> rst = riskBffService.getDrivingLicense();
			if (!ResponseCodeEnum.SUCCESS.getCode().equals(rst.getCode())) {
				throw new BizException(OrderBffExceptionCodeEnum.CANNOT_RELET.getCode(), "抱歉，因您的驾驶证未通过，暂时不能续订。");
			}

			// 生成类型
			createVo.setCreateType(OrderCreateTypeEnum.PURCHASED);
			// 新增
			result.success(this.createOrder(token, source.toUpperCase(), deviceNum, createVo));

		} catch (BizException e) {
			logger.error("[createOrderPurchase]新增加购订单异常:", e);
			result.error(e.getExceptionCode() == null ? ResponseCodeEnum.UNKNOWN_ERROR.getCode() : e.getExceptionCode(),
					e.getMessage());
		} catch (Exception e) {
			logger.error("[createOrderPurchase]新增加购订单异常:", e);
			result.error("新增订单异常");
		}

		return result;
	}

	@Override
	@ApiOperation(value = "生成续订订单")
	@PostMapping(value = "/renew/create")
	public ResponseResult<OrderVo> createOrderRenew(@RequestHeader(value = "token", required = true) String token,
			@RequestHeader(value = "channel", required = true) String source,
			@RequestHeader(value = "deviceNum", required = true) String deviceNum,
			@RequestBody @Valid OrderRenewCreateVo createVo, BindingResult bindingResult) {
		ResponseResult<OrderVo> result = new ResponseResult<>();
		try {
			// 验证驾驶证
			ResponseResult<String> rst = riskBffService.getDrivingLicense();
			if (!ResponseCodeEnum.SUCCESS.getCode().equals(rst.getCode())) {
				throw new BizException(OrderBffExceptionCodeEnum.CANNOT_RELET.getCode(), "抱歉，因您的驾驶证未通过，暂时不能续订。");
			}
			// 生成类型
			createVo.setCreateType(OrderCreateTypeEnum.RENEW);
			// 新增
			result.success(this.createOrder(token, source.toUpperCase(), deviceNum, createVo));
		} catch (BizException e) {
			logger.error("[createOrderRenew]新增续订订单异常:", e);
			result.error(e.getExceptionCode() == null ? ResponseCodeEnum.UNKNOWN_ERROR.getCode() : e.getExceptionCode(),
					e.getMessage());
		} catch (Exception e) {
			logger.error("[createOrderRenew]新增续订订单异常:", e);
			result.error("新增订单异常");
		}

		return result;
	}

	private OrderVo createOrder(String token, String source, String deviceNum, OrderCreateVo createVo)
			throws BizException, Exception {
		OrderVo result = new OrderVo();
		// 根据token获取用户信息
		UserVo user = tokenUtil.getUser(token);
		// 订单来源
		createVo.setSource(Enum.valueOf(OrderSourceEnum.class, source));
		// 操作人信息(用户信息)
		createVo.setOperateId(user.getUserId());
		createVo.setOperateName(user.getRealName());
		createVo.setOperateIdNumber(user.getIdNumber());
		createVo.setPhone(user.getPhone());
		// 设备号
		createVo.setDeviceNum(deviceNum);
		// 新增
		result = orderTradeBffService.createOrder(createVo);
		try {
			if (result != null && result.getOrderCode() != null) {
				// 修改用户未下单用户
				if (OrderCreateTypeEnum.EXCLUSIVEL.equals(createVo.getCreateType())
						|| OrderCreateTypeEnum.SUBSCRIBE.equals(createVo.getCreateType())) {
					// 生成专属订单、生成订阅订单
					userBffService.modifyOrderUser(user.getUserId());
				}
			}
		} catch (Exception e) {
			logger.error("[createOrder]生成订单成功，修改用户未下单用户异常");
		}

		return result;
	}


	@Override
	@ApiOperation(value = "订单取消")
	@PostMapping(value = "/cancel")
	public ResponseResult<Boolean> cancel(@RequestHeader(value = "token", required = true) String token,
			@RequestBody @Valid TradeCancelModifyVo modifyVo, BindingResult bindingResult) {
		ResponseResult<Boolean> result = new ResponseResult<>();
		try {
			// 根据token获取用户信息
			UserVo user = tokenUtil.getUser(token);
			modifyVo.setOperateId(user.getUserId());
			modifyVo.setOperateName(user.getRealName());
			// 取消
			result.success(orderTradeBffService.modifyCancel(modifyVo));
		} catch (BizException e) {
			logger.error("[cancel]订单取消失败:", e);
			result.error(e.getExceptionCode() == null ? ResponseCodeEnum.UNKNOWN_ERROR.getCode() : e.getExceptionCode(),
					e.getMessage());
		} catch (Exception e) {
			logger.error("[cancel]订单取消异常:", e);
			result.error("订单取消异常");
		}

		return result;
	}

	@Override
	@ApiOperation(value = "验证是否可加购")
	@ApiImplicitParams({ @ApiImplicitParam(name = "orderCode", value = "订单号", required = true, dataType = "Long") })
	@GetMapping(value = "/purchase/verify")
	public ResponseResult<ReletVerifyVo> verifyPurchase(@RequestHeader(value = "token", required = true) String token,
			Long orderCode) {
		ResponseResult<ReletVerifyVo> result = new ResponseResult<>();
		try {
			// 验证
			result.success(orderBffService.getVerifyPurchase(orderCode));

		} catch (BizException e) {
			logger.error("[verifyPurchase]验证是否可加购失败:", e);
			result.error(e.getExceptionCode() == null ? ResponseCodeEnum.UNKNOWN_ERROR.getCode() : e.getExceptionCode(),
					e.getMessage());
		} catch (Exception e) {
			logger.error("[verifyPurchase]验证是否可加购异常:", e);
			result.error("验证是否可加购异常");
		}

		return result;
	}

	@Override
	@ApiOperation(value = "验证是否可续订")
	@ApiImplicitParams({ @ApiImplicitParam(name = "orderCode", value = "订单号", required = true, dataType = "Long") })
	@GetMapping(value = "/renew/verify")
	public ResponseResult<ReletVerifyVo> verifyRenew(@RequestHeader(value = "token", required = true) String token,
			Long orderCode) {
		ResponseResult<ReletVerifyVo> result = new ResponseResult<>();
		try {
			// 验证
			result.success(orderBffService.getVerifyRenew(orderCode));

		} catch (BizException e) {
			logger.error("[verifyRenew]验证是否可续订失败:", e);
			result.error(e.getExceptionCode() == null ? ResponseCodeEnum.UNKNOWN_ERROR.getCode() : e.getExceptionCode(),
					e.getMessage());
		} catch (Exception e) {
			logger.error("[verifyRenew]验证是否可续订异常:", e);
			result.error("验证是否可续订异常");
		}

		return result;
	}

	@Override
	@ApiOperation(value = "验车报告确认")
	@PostMapping(value = "/report/confirm")
	public ResponseResult<ReportVo> reportConfirm(@RequestHeader(value = "token", required = true) String token,
			@RequestBody @Valid ReportModifyVo modifyVo, BindingResult bindingResult) {
		ResponseResult<ReportVo> result = new ResponseResult<>();
		try {
			// 验证
			result.success(orderReturnBffService.modifyReportConfirm(modifyVo));

		} catch (BizException e) {
			logger.error("[reportConfirm]验车报告确认操作失败:", e);
			result.error(e.getExceptionCode() == null ? ResponseCodeEnum.UNKNOWN_ERROR.getCode() : e.getExceptionCode(),
					e.getMessage());
		} catch (Exception e) {
			logger.error("[reportConfirm]验车报告确认操作异常:", e);
			result.error("验车报告确认异常");
		}

		return result;
	}

	@Override
	@ApiOperation(value = "验车报告预览")
	@GetMapping(value = "/report/preview")
	public ResponseResult<CheckReportVo> checkReport(@RequestHeader(value = "token", required = true) String token,
												Long orderCode) {
		ResponseResult<CheckReportVo> result = new ResponseResult<>();
		try {
			result.success(orderReturnBffService.getCheckReport(orderCode));

		} catch (BizException e) {
			logger.error("[checkReport]验车报告预览异常:", e);
			result.error(e.getExceptionCode() == null ? ResponseCodeEnum.UNKNOWN_ERROR.getCode() : e.getExceptionCode(),
					e.getMessage());
		} catch (Exception e) {
			logger.error("[checkReport]验车报告预览异常:", e);
			result.error("验车报告预览异常");
		}
		return result;
	}

	@Override
	@ApiOperation(value = "大屏订单信息")
	@GetMapping(value = "/aio")
	public ResponseResult<List<OrderAioVo>> aioOrder(@RequestHeader(value = "token", required = true) String token,
			@RequestHeader(value = "deviceNum", required = true) String deviceNum) {
		ResponseResult<List<OrderAioVo>> result = new ResponseResult<>();
		try {
			// 根据token获取用户信息
			UserVo user = tokenUtil.getUser(token);
			// 验证
			result.success(orderBffService.getAio(user, deviceNum));

		} catch (BizException e) {
			logger.error("[aioOrder]查询大屏订单信息失败:", e);
			result.error(e.getExceptionCode() == null ? ResponseCodeEnum.UNKNOWN_ERROR.getCode() : e.getExceptionCode(),
					e.getMessage());
		} catch (Exception e) {
			logger.error("[aioOrder]查询大屏订单信息异常:", e);
			result.error("查询订单信息异常");
		}
		return result;
	}

	@Override
	@ApiOperation(value = "大屏订单提车")
	@PostMapping(value = "/delivery")
	public ResponseResult<DeliveryResultVo> delivery(@RequestHeader(value = "token", required = true) String token,
			@RequestHeader(value = "counselorToken", required = true) String counselorToken,
			@RequestHeader(value = "deviceNum", required = true) String deviceNum,
			@RequestBody @Valid TradeDeliveryModifyVo modifyVo, BindingResult bindingResult) {
		ResponseResult<DeliveryResultVo> result = new ResponseResult<>();
		try {
			// 根据token获取用户信息
			UserVo user = tokenUtil.getUser(token);
			modifyVo.setUserId(user.getUserId());
			modifyVo.setDeviceNum(deviceNum);
			AccountVo accountVo = tokenUtil.getCounselorUser(counselorToken);
			modifyVo.setSalesmanId(accountVo.getId());
			modifyVo.setSalesmanName(accountVo.getRealName());
			// 提车
			DeliveryResultVo deliveryResultVo = orderTradeBffService.modifyDelivery(modifyVo);

			if (deliveryResultVo.getResult()) {
				// 成功提车，生成合同(异步)
				contractBffService.createContractByDelivery(modifyVo);
			}

			result.success(deliveryResultVo);
		} catch (BizException e) {
			logger.error("[delivery]大屏订单提车失败:", e);
			result.error(e.getExceptionCode() == null ? ResponseCodeEnum.UNKNOWN_ERROR.getCode() : e.getExceptionCode(),
					e.getMessage());
		} catch (Exception e) {
			logger.error("[delivery]大屏订单提车异常:", e);
			result.error("订单提车异常");
		}
		return result;
	}

	@Override
	@ApiOperation(value = "大屏订单还车")
	@PostMapping(value = "/return/car")
	public ResponseResult<ReturnCarResultVo> returnCar(@RequestHeader(value = "token", required = true) String token,
			@RequestHeader(value = "counselorToken", required = true) String counselorToken,
			@RequestHeader(value = "deviceNum", required = true) String deviceNum,
			@RequestBody @Valid ReturnCarModifyVo modifyVo, BindingResult bindingResult) {
		ResponseResult<ReturnCarResultVo> result = new ResponseResult<>();
		try {
			// 根据token获取用户信息
			UserVo user = tokenUtil.getUser(token);
			modifyVo.setUserId(user.getUserId());
			modifyVo.setDeviceNum(deviceNum);
			AccountVo accountVo = tokenUtil.getCounselorUser(counselorToken);
			modifyVo.setSalesmanId(accountVo.getId());
			modifyVo.setSalesmanName(accountVo.getRealName());
			// 还车
			ReturnCarResultVo returnCarResultVo = orderReturnBffService.modifyReturnCar(modifyVo);
			if (returnCarResultVo.getResult()) {
				// 还车成功，异步生成还车单
				contractBffService.createContractByReturnCar(modifyVo);
			}

			result.success(returnCarResultVo);
		} catch (BizException e) {
			logger.error("[returnCar]大屏订单还车失败:", e);
			result.error(e.getExceptionCode() == null ? ResponseCodeEnum.UNKNOWN_ERROR.getCode() : e.getExceptionCode(),
					e.getMessage());
		} catch (Exception e) {
			logger.error("[returnCar]大屏订单还车异常:", e);
			result.error("订单还车异常");
		}
		return result;
	}

	@Override
	@ApiOperation(value = "查询订单还车结果")
	@ApiImplicitParams({ @ApiImplicitParam(name = "orderCode", value = "订单号", required = true, dataType = "Long") })
	@GetMapping(value = "/return/car/result")
	public ResponseResult<ReturnCarResultVo> returnCarResult(@RequestParam Long orderCode) {
		ResponseResult<ReturnCarResultVo> result = new ResponseResult<>();
		try {

			result.success(orderReturnBffService.getReturnCarResult(orderCode));
		} catch (BizException e) {
			logger.error("[returnCarResult]订单还车结果查询失败:", e);
			result.error(e.getExceptionCode() == null ? ResponseCodeEnum.UNKNOWN_ERROR.getCode() : e.getExceptionCode(),
					e.getMessage());
		} catch (Exception e) {
			logger.error("[returnCarResult]订单还车结果查询异常:", e);
			result.error("订单还车结果查询异常");
		}
		return result;
	}

	@ApiOperation(value = "取消退租")
	@PostMapping(value = "/renege/cancel")
	@Override
	public ResponseResult<Boolean> renegeCancel(@RequestHeader(value = "token", required = true) String token,
			@RequestBody @Valid RenegeCancelModifyVo modifyVo, BindingResult bindingResult) {
		ResponseResult<Boolean> result = new ResponseResult<>();
		try {
			if (orderRenegeBffService.modifyCancel(modifyVo)) {
				result.success(true);
			} else {
				result.error("取消退租失败", false);
			}
		} catch (BizException e) {
			logger.error("[cancelRenege]取消退租失败:", e);
			result.error(e.getExceptionCode() == null ? ResponseCodeEnum.UNKNOWN_ERROR.getCode() : e.getExceptionCode(),
					e.getMessage());
		} catch (Exception e) {
			logger.error("[cancelRenege]取消退租异常:", e);
			result.error("取消退租异常");
		}
		return result;
	}

	@ApiOperation(value = "查询订单退租人信息")
	@ApiImplicitParams({ @ApiImplicitParam(name = "orderCode", value = "订单号", required = true, dataType = "Long") })
	@GetMapping(value = "/renege/payee/query")
	@Override
	public ResponseResult<OrderPayeeVo> orderPayee(@RequestHeader(value = "token", required = true) String token,
			@RequestParam Long orderCode) {
		ResponseResult<OrderPayeeVo> result = new ResponseResult<>();
		try {
			// 根据token获取用户信息
			UserVo user = tokenUtil.getUser(token);
			result.success(orderRenegeBffService.getOrderPayee(orderCode, user.getUserId()));
		} catch (BizException e) {
			logger.error("[orderPayee]查询订单退租人信息失败:", e);
			result.error(e.getExceptionCode() == null ? ResponseCodeEnum.UNKNOWN_ERROR.getCode() : e.getExceptionCode(),
					e.getMessage());
		} catch (Exception e) {
			logger.error("[orderPayee]查询订单退租人信息异常:", e);
			result.error("查询订单退租人信息异常");
		}
		return result;
	}

	@ApiOperation(value = "订单退租退款人信息新增")
	@PostMapping(value = "/renege/payee/create")
	@Override
	public ResponseResult<Boolean> orderPayee(@RequestHeader(value = "token", required = true) String token,
			@RequestBody @Valid OrderPayeeCreateVo createVo, BindingResult bindingResult) {
		ResponseResult<Boolean> result = new ResponseResult<>();
		try {
			// 根据token获取用户信息
			UserVo user = tokenUtil.getUser(token);
			createVo.setUserId(user.getUserId());
			if (orderRenegeBffService.createPayee(createVo)) {
                // 生成解除协议
                contractBffService.createContractByRenege(createVo.getOrderCode(), createVo.getUserId());
                result.success(true);
            } else {
                result.error("退款人信息新增失败", false);
            }
		} catch (BizException e) {
			logger.error("[orderPayee]退款人信息新增失败:", e);
			result.error(e.getExceptionCode() == null ? ResponseCodeEnum.UNKNOWN_ERROR.getCode() : e.getExceptionCode(),
					e.getMessage());
		} catch (Exception e) {
			logger.error("[orderPayee]退款人信息新增异常:", e);
			result.error("退款人信息新增异常");
		}
		return result;
	}

	@ApiOperation(value = "查询退租解除协议参数")
	@ApiImplicitParams({@ApiImplicitParam(name = "orderCode", value = "订单号", required = true, dataType = "Long")})
	@GetMapping(value = "/renege/contract/params")
	@Override
	public ResponseResult<ContractRenegeParamsVo> renegeContractParams(@RequestHeader(value = "token", required = true) String token,
	                                                                   @RequestParam Long orderCode) {
		ResponseResult<ContractRenegeParamsVo> result = new ResponseResult<>();
		try {
            // 根据token获取用户信息
            UserVo user = tokenUtil.getUser(token);

            result.success(contractBffService.getContractRenegeParams(orderCode, user.getUserId(), true));
        } catch (BizException e) {
			logger.error("[orderPayee]查询退租解除协议参数失败:", e);
			result.error(e.getExceptionCode() == null ? ResponseCodeEnum.UNKNOWN_ERROR.getCode() : e.getExceptionCode(),
					e.getMessage());
		} catch (Exception e) {
			logger.error("[orderPayee]查询退租解除协议参数异常:", e);
			result.error("查询退租解除协议参数异常");
		}
		return result;
	}


}
